clear all
use YOUR_DIRECTORY\cces_panel_download.dta

rename (CC10_321		CC10_325	gender_10	race_10		pid3_10	pid7_10	educ_10	birthyr_10	inputstate_10	newsint_10		ideo5_10)(climatechange2010		environment2010	gender2010	race2010		partyid32010	partyid72010	education2010	birthyear2010	statefips2010	news2010	ideology2010)
rename (CC12_321		CC12_325	gender_12	race_12		pid3_12	pid7_12	educ_12	birthyr_12	inputstate_12	newsint_12		ideo5_12)(climatechange2012		environment2012	gender2012	race2012		partyid32012	partyid72012	education2012	birthyear2012	statefips2012	news2012    ideology2012)
rename (CC14_321		CC14_325	gender_14	race_14		pid3_14	pid7_14	educ_14	birthyr_14	inputstate_14	newsint_14		ideo5_14)(climatechange2014		environment2014	gender2014	race2014		partyid32014	partyid72014	education2014	birthyear2014	statefips2014	news2014    ideology2014)

gen weight2010=weight
gen weight2012=weight
gen weight2014=weight

keep caseid weight2010 weight2012 weight2014 climatechange2010		environment2010	gender2010	race2010		partyid32010	partyid72010	education2010	birthyear2010	statefips2010	news2010		ideology2010 	climatechange2012		environment2012	gender2012	race2012		partyid32012	partyid72012	education2012	birthyear2012	statefips2012	news2012		ideology2012  climatechange2014	environment2014	gender2014	race2014		partyid32014	partyid72014	education2014	birthyear2014	statefips2014	news2014		ideology2014

replace partyid32010=. if partyid32010>3
replace partyid32012=. if partyid32012>3
replace partyid32012=. if partyid32012>3

drop if partyid32010==.
drop if partyid32012==.
drop if partyid32014==.


 gen s2012=1 if partyid32010!=partyid32012
 replace s2012=0 if partyid32010==partyid32012
 
 gen s2014=1 if partyid32012!=partyid32014
 replace s2014=0 if partyid32012==partyid32014
 
 gen s2014_alt=1 if partyid32010!=partyid32014
 replace s2014_alt=0 if partyid32010==partyid32014
 
 gen sch2012=1 if climatechange2010!=climatechange2012
 replace sch2012=0 if climatechange2010==climatechange2012
 
 gen sch2014=1 if climatechange2012!=climatechange2014
 replace sch2014=0 if climatechange2012==climatechange2014
 
gen st_switch=1 if statefips2010!=statefips2012
replace st_switch=1 if statefips2012!=statefips2014
drop if st_switch==1

gen repub_2010=1 if partyid32010==2
replace repub_2010 =0 if partyid32010==1
replace repub_2010 =0 if partyid32010==3

gen repub_2012=1 if partyid32012==2
replace repub_2012 =0 if partyid32012==1
replace repub_2012 =0 if partyid32012==3

gen ind_2010=1 if partyid32010==3
replace ind_2010 =0 if partyid32010==2
replace ind_2010 =0 if partyid32010==1

gen ind_2012=1 if partyid32012==3
replace ind_2012 =0 if partyid32012==2
replace ind_2012 =0 if partyid32012==1

gen democ_2010=1 if partyid32010==1
replace democ_2010 =0 if partyid32010==2
replace democ_2010 =0 if partyid32010==3

gen democ_2012=1 if partyid32012==1
replace democ_2012 =0 if partyid32012==2
replace democ_2012 =0 if partyid32012==3

gen r_defect2012=1 if repub_2010==1& partyid32012==1
replace r_defect2012=1 if repub_2010==1& partyid32012==3
replace r_defect2012=0 if repub_2010==1&partyid32012==2

gen ind_defect2012=1 if ind_2010==1& partyid32012==1
replace ind_defect2012=1 if ind_2010==1& partyid32012==2
replace ind_defect2012=0 if ind_2010==1&partyid32012==3


reshape long weight climatechange environment gender race partyid3 partyid7 education birthyear statefips news ideology, i(caseid) j(year_match)

xtset caseid year_match

merge m:1 statefips using statefips.dta

drop _merge

rename stateid state

merge m:1 state year_match using allstates_formerge_final

keep if _merge==3

gen r_win=0 if m_percent!=.
replace r_win=1 if m_percent>0 

gen gw_belief=climatechange
replace gw_belief=1 if climatechange<=2 & climatechange!=.
replace gw_belief=0 if climatechange>=3 & climatechange!=.


replace partyid3=. if partyid3>=4

replace ideology=. if ideology==6

gen democrat=0 if partyid3!=.
replace democrat=1 if partyid3==1

gen republican=0 if partyid3!=.
replace republican=1 if partyid3==2

gen independent=0 if partyid3!=.
replace independent=1 if partyid3==3


tab year_match, gen(yfe)

drop if republican==. | r_win==. | gw_belief==.

bysort caseid: egen rep_m=mean(republican)
bysort caseid: egen r_m=mean(r_win)
bysort caseid: egen fe12m=mean(yfe2)
bysort caseid: egen fe14m=mean(yfe3)
bysort caseid: egen gw_m=mean(gw_belief)

gen d2rep=republican-rep_m
gen d2r_m=r_win-r_m
gen d2fe12=yfe2-fe12m
gen d2fe14=yfe3-fe14m
gen d2gw=gw_belief-gw_m

save cces_panel_foranalysis
************Data are ready******************
